<template>
  <!--  巡检计划-新增  -->
  <div v-dialog-drag>
    <el-dialog title="新增" :visible.sync="dialogVisible" width="30%">
      <el-form ref="ruleForm" :model="form" :rules="rule" label-width="100px">
        <el-form-item label="站点" prop="site_id">
          <el-select v-model="form.site_id" size="mini" placeholder="请选择" style="width: 100%">
            <el-option
              v-for="item in site_options"
              :key="item.id"
              :label="item.name"
              :value="item.id">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="计划标题" prop="plan_title">
          <el-input type="text" size="mini" v-model="form.plan_title" placeholder="请输入计划标题"></el-input>
        </el-form-item>
        <el-form-item label="计划内容" prop="plan_content">
          <el-input type="textarea" size="mini" v-model="form.plan_content" placeholder="请输入计划内容"></el-input>
        </el-form-item>
        <el-form-item label="计划周期" prop="plan_cycle">
          <el-select v-model="form.plan_cycle" size="mini" placeholder="请选择" style="width: 100%">
            <el-option
              v-for="item in cycle_options"
              :key="item.cycle_value"
              :label="item.cycle_label"
              :value="item.cycle_value">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item class="demonstration" prop="plan_start" label="开始时间">
          <el-date-picker
            v-model="form.plan_start"
            type="date"
            size="mini"
            format="yyyy-MM-dd"
            value-format="yyyy-MM-dd"
            placeholder="选择日期"
            style="width: 100%">
          </el-date-picker>
        </el-form-item>
        <el-form-item class="demonstration" prop="plan_end" label="结束时间">
          <el-date-picker
            size="mini"
            v-model="form.plan_end"
            type="date"
            format="yyyy-MM-dd"
            value-format="yyyy-MM-dd"
            placeholder="选择日期"
            style="width: 100%">
          </el-date-picker>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button type="primary" size="mini" @click="resetForm">取消</el-button>
        <el-button type="primary" size="mini" @click="addOrEdit">确定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { allSite, createPlan } from '@/api/operations'
export default {
  name: 'inspectionPlanEdit',
  data() {
    return {
      dialogVisible: false,
      form:{
        plan_type: 1,
        site_id:'',
        plan_title:'',
        plan_content:'',
        plan_cycle:'',
        plan_start:'',
        plan_end:''
      },
      site_options: [], // 站点数据
      cycle_options: [{
        cycle_value: '1',
        cycle_label: '单次'
      }, {
        cycle_value: '2',
        cycle_label: '每周'
      }, {
        cycle_value: '3',
        cycle_label: '每月'
      }],
      rule: {
        site_id: [
          {required: true, message: '请选择站点', trigger: 'blur'}
        ],
        plan_title: [
          {required: true, message: '请输入计划名称', trigger: 'blur'}
        ],
        plan_content: [
          {required: true, message: '请输入计划内容', trigger: 'blur'}
        ],
        plan_cycle: [
          {required: true, message: '请选择计划周期', trigger: 'blur'}
        ],
        plan_start: [
          {required: true, message: '请选择计划开始时间', trigger: 'blur'}
        ],
        plan_end: [
          {required: true, message: '请选择计划结束时间', trigger: 'blur'}
        ]
      }
    }
  },
  created() {
    this.getAllSite()
  },
  methods: {
    /**
     * @description: 平台端获取所有站点
     * @author: 书童
     */
    getAllSite() {
      allSite().then(res => {
        this.site_options = res.data['data']
      })
    },
    // 新增或编辑
    addOrEdit() {
      this.$refs['ruleForm'].validate((valid) => {
        if (valid) {
          createPlan(this.form).then(res => {
            this.$message({
              message: '新增成功',
              type: 'success'
            })
            this.$emit('getList')
            this.resetForm()
          })
        }
      })

      console.log(this.form)
    },
    // 打开弹窗
    open(row) {
      // 修改打开弹窗的变量为true，就可以打开弹窗了
      this.dialogVisible = true;
    },
    // 关闭弹窗的方法
    close() {
      // 关闭弹窗的变量为false，就可以关闭弹窗了
      this.dialogVisible = false;
      // this.$emit('close')
    },
    // 表单重置
    resetForm() {
      this.$refs['ruleForm'].resetFields()
      this.dialogVisible = false;
    }

  }
}
</script>

<style lang="scss" scoped>

</style>
